use rbatis::rbdc::datetime::DateTime;
use rocket::serde::json::{serde_json::json, Value};
use rocket::get;
use crate::domain::order::Order;
use crate::domain::goods_shop_cart::GoodsShopcart;
use crate::domain::goods::Goods;
use rocket::serde::{Deserialize};
use serde::Serialize;
use crate::controller::user_shopping::Cart;
use crate::RB;
use crate::response::response_object::RespData;

#[derive(Deserialize,Serialize)]
struct ResOrder {
    carts: Vec<Cart>,
    time:DateTime,
}

#[get("/wxUser/getAllOrder/<user_id>")]
pub async fn get_all_order(user_id: u32) -> Value {
    let mut rb = RB.clone();
    let list = Order::select_by_user_id(user_id).await;
    let mut res_order:Vec<ResOrder> = vec![];
    for item in list {
        let carts_id = item.to_owned().carts_id.unwrap();
        let time = item.to_owned().time.unwrap();
        let carts_id: Vec<&str> = carts_id.split(",").collect();
        let mut  vec_cart:Vec<Cart> = vec![];
        for cart_id in carts_id {
            let cart = GoodsShopcart::select_by_column(&mut rb, "id", cart_id)
                .await
                .unwrap()
                .get(0).unwrap().to_owned();
            let goods = Goods::select_by_column(&mut rb, "id", cart.goods_id)
                .await


                .unwrap().get(0).unwrap().to_owned();
            let final_cart = Cart{
                good:goods,
                num: cart.num.unwrap(),
                status: cart.status.unwrap(),
                id: cart.id.unwrap(),
            };
            vec_cart.push(final_cart);
        }
        let p = ResOrder{
            carts: vec_cart,
            time,
        };
        res_order.push(p)
    }
    json!(RespData::success(res_order))
}